package com.zpark.controller.exception;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xiaoyu
 * @version 1.0 2022/10/8
 */
@ControllerAdvice
public class GlobalExceptionHandlerController {
//    @ExceptionHandler(Exception.class)
//    @ResponseBody
//    public Map<String, Object> handlerException(Throwable e) {
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("code", "500");
//        map.put("msg", e.getMessage());
//        return map;
//    }


    @ExceptionHandler(Exception.class)
    public ModelAndView handlerException(Throwable e) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("500");
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg", e.getMessage());
        map.put("code", 500);
        mv.addObject("result", map);
        return mv;
    }

}
